package com.cheyun.netsalev3.viewmodel

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.graphics.drawable.ColorDrawable
import android.os.Environment
import android.os.Handler
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.bean.*
import com.cheyun.netsalev3.repository.AddDriveActivityRepository
import com.cheyun.netsalev3.view.mars.choosecar.MarsChooseCarActivity
import com.cheyun.netsalev3.widget.MyContentDialog
import com.cheyun.netsalev3.widget.MyContentDialogLayout
import com.cheyun.netsalev3.widget.PopDialog
import java.io.Serializable
import android.print.PrintAttributes
import android.print.PrintManager
import com.cheyun.netsalev3.adapter.MyPrintAdapter
import com.cheyun.netsalev3.utils.DownloadUtil
import java.io.File
import android.os.Looper
import android.view.Gravity
import android.widget.ImageView
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.CloneUtils
import com.cheyun.netsalev3.bean.basedata.BaseDataParam
import com.cheyun.netsalev3.bean.basedata.Expchar
import com.cheyun.netsalev3.bean.basedata.Labeltype
import com.cheyun.netsalev3.bean.drive.Experience2
import com.cheyun.netsalev3.bean.drive.ReserveInfo
import com.cheyun.netsalev3.bean.drive.Reservemit
import com.cheyun.netsalev3.bean.task.TaskListData
import com.cheyun.netsalev3.repository.TaskListActivityRepository
import com.cheyun.netsalev3.tencenttim.utils.DemoLog
import com.cheyun.netsalev3.utils.CommonFunc
import com.cheyun.netsalev3.utils.FunctionUtils
import com.cheyun.netsalev3.utils.MySharedPreferences
import com.cheyun.netsalev3.utils.api.ApiStores
import com.cheyun.netsalev3.view.CustomerTagActivity
import com.cheyun.netsalev3.view.drivelist.DriveListActivity
import com.dou361.dialogui.DialogUIUtils
import com.google.gson.Gson


class CustomerTagActivityViewModel(ids:String) : BaseViewModel(){

    var listdata = MutableLiveData<List<Labeltype>>()

    var baseDataParam: BaseDataParam? = MySharedPreferences.getBaseData()

    init {
        var d = ArrayList<Labeltype>()
        var idList = ids.split(',').toList()
        baseDataParam?.apply {
            if(this.labeltype!=null){
                for (item in this.labeltype) {
                    var labelType = CloneUtils.deepClone(item,Labeltype::class.java)
                    var labels = arrayListOf<DialogParam>()
                    var value = ""
                    for(label in this.label){
                        if(label.type_id == item.id){
                            var isSelect = false
                            isSelect = idList.contains(label.id.toString())
                            if(isSelect) value = label.id.toString()
                            labels.add(DialogParam(label.title,label.id.toString(),"1",isSelect))
                        }
                    }
                    labelType.list = labels
                    labelType.value = value
                    d.add(labelType)
                }
            }
        }
        listdata.postValue(d)
    }

    fun onSave(view:View){
        var actvity = view.context as CustomerTagActivity
        var idList = arrayListOf<String>()
        var titleList = arrayListOf<DialogParam>()
        var d = listdata.value
        d?.forEach {
            if(it.value.isNotEmpty()) {
                idList.add(it.value)
                for(lable in it.list){
                    if(lable.id.equals(it.value) ){
                        titleList.add(lable)
                    }
                }
            }
        }

        var intent = Intent()
        intent.putExtra("ids",idList.joinToString(","))
        intent.putExtra("list", titleList as Serializable)
        actvity.setResult(106, intent)
        actvity.finish()
    }

    fun radioGroupCallback(pos:Any,data:DialogParam, from:String){
        var d = listdata.value
        d?.forEach {
          if(it.id == pos){
              it.value = data.id
          }
        }
    }




}